//
//  ViewController.m
//  DelegateStudy
//
//  Created by Jason Len on 2019/9/4.
//  Copyright © 2019年 Jason. All rights reserved.
//

#import "ViewController.h"
#import "GTNormalTableViewCell.h"
#import "GTDetailViewController.h"
#import "GTDeleteCellView.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,GTNormalTableViewCellDelegate>

@property(nonatomic,strong,readwrite) NSMutableArray *dataArray;
@property(nonatomic,strong,readwrite) UITableView *tableView;

@end

@implementation ViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.tabBarItem.title=@"新闻";
        self.view.backgroundColor = [UIColor blueColor];
        self.tabBarItem.image=[UIImage imageNamed:@"icon.bundle/page@2x.png"];
        self.tabBarItem.selectedImage=[UIImage imageNamed:@"icon.bundle/page_selected@2x.png"];
        self.view.backgroundColor = [UIColor whiteColor];
        
        _dataArray = @[].mutableCopy;
        for (int i=0; i<20; i++){
            [_dataArray addObject: @(i)];
        }
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:_tableView];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GTNormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id"];
    if(!cell){
        cell = [[GTNormalTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"id"];
    }
    //设置 delegate=self
    cell.delegate = self;
    [cell layoutTableViewCell];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if(indexPath.row % 2 == 0){
        return 100;
//    }else{
//        return 50;
//    }
}
//实现delegate函数
-(void)tableViewCell:(UITableViewCell *)tableViewCell clickDeleteButton:(UIButton *)deleteButton{
    GTDeleteCellView *deleteView = [[GTDeleteCellView alloc]initWithFrame:self.view.bounds];
    //将deleteButton的坐标系转换到window坐标系中
   __weak typeof(self)wSelf = self;
    CGRect rect= [tableViewCell convertRect:deleteButton.frame toView:nil];
    [deleteView showDeleteViewFromPoint:rect.origin clickBlock:^{
        
        __strong typeof(self)strongSelf=wSelf;
        [strongSelf.dataArray removeLastObject];
        [strongSelf.tableView deleteRowsAtIndexPaths:@[[strongSelf.tableView indexPathForCell:tableViewCell]] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@", [NSString stringWithFormat:@"didSelect -%@",@(indexPath.row)]);
    
    GTDetailViewController *viewController = [[GTDetailViewController alloc]init];
    viewController.navigationItem.title = [NSString stringWithFormat:@"kkkk - %@",@(indexPath.row)];
    
    [self.navigationController pushViewController:viewController animated:YES];
    
    
    
}

@end
